package pixelradar;

import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.ColorizerFoliage;
import net.minecraft.world.ColorizerGrass;
import net.minecraft.world.biome.Biome;
import pixelradar.region.BlockColours;
import pixelradar.util.Logging;
import pixelradar.util.Render;
import pixelradar.util.Texture;
import pixelradar.util.Utils;

import java.util.Iterator;

public class BlockColourGen {
    private static int getIconMapColour(TextureAtlasSprite icon, Texture terrainTexture) {
        int iconX = Math.round((float) terrainTexture.w * Math.min(icon.getMinU(), icon.getMaxU()));
        int iconY = Math.round((float) terrainTexture.h * Math.min(icon.getMinV(), icon.getMaxV()));
        int iconWidth = Math.round((float) terrainTexture.w * Math.abs(icon.getMaxU() - icon.getMinU()));
        int iconHeight = Math.round((float) terrainTexture.h * Math.abs(icon.getMaxV() - icon.getMinV()));
        int[] pixels = new int[iconWidth * iconHeight];
        terrainTexture.getRGB(iconX, iconY, iconWidth, iconHeight, pixels, 0, iconWidth, icon);
        return Render.getAverageColourOfArray(pixels);
    }

    private static void genBiomeColours(BlockColours bc) {
        Iterator var1 = Biome.REGISTRY.iterator();

        while (var1.hasNext()) {
            Biome biome = (Biome) var1.next();
            if (biome != null) {
                double temp = (double) MathHelper.clamp(biome.getDefaultTemperature(), 0.0F, 1.0F);
                double rain = (double) MathHelper.clamp(biome.getRainfall(), 0.0F, 1.0F);
                int grasscolor = ColorizerGrass.getGrassColor(temp, rain);
                int foliagecolor = ColorizerFoliage.getFoliageColor(temp, rain);
                int watercolor = biome.getWaterColorMultiplier();
                bc.setBiomeData(biome.getBiomeName(), watercolor & 16777215, grasscolor & 16777215, foliagecolor & 16777215);
            }
        }

    }

    public static void genBlockColours(BlockColours bc) {
        Logging.log("generating block pixelradar.map colours from textures");
        int terrainTextureId = Minecraft.getMinecraft().renderEngine.getTexture(TextureMap.LOCATION_BLOCKS_TEXTURE).getGlTextureId();
        if (terrainTextureId == 0) {
            Logging.log("error: could get terrain texture ID");
        } else {
            Texture terrainTexture = new Texture(terrainTextureId);
            float u1Last = 0.0F;
            float u2Last = 0.0F;
            float v1Last = 0.0F;
            float v2Last = 0.0F;
            int blockColourLast = 0;
            int eCount = 0;
            int bCount = 0;
            int sCount = 0;
            Iterator var11 = Block.REGISTRY.iterator();

            while (var11.hasNext()) {
                Block block = (Block) var11.next();

                for (int dv = 0; dv < 16; ++dv) {
                    int blockColour = 0;
                    if (block.getRenderType(block.getDefaultState()) != EnumBlockRenderType.INVISIBLE) {
                        TextureAtlasSprite icon = null;

                        try {
                            icon = Minecraft.getMinecraft().getBlockRendererDispatcher().getBlockModelShapes().getTexture(block.getStateFromMeta(dv));
                        } catch (Exception var20) {
                            ++eCount;
                        }

                        if (icon != null) {
                            float u1 = icon.getMinU();
                            float u2 = icon.getMaxU();
                            float v1 = icon.getMinV();
                            float v2 = icon.getMaxV();
                            if (Utils.judgeFloat(u1, u1Last) && Utils.judgeFloat(u2, u2Last) && Utils.judgeFloat(v1, v1Last) && Utils.judgeFloat(v2, v2Last)) {
                                blockColour = blockColourLast;
                                ++sCount;
                            } else {
                                blockColour = getIconMapColour(icon, terrainTexture);
                                if (((ResourceLocation) Block.REGISTRY.getNameForObject(block)).getResourceDomain().contains("CarpentersBlocks")) {
                                }

                                u1Last = u1;
                                u2Last = u2;
                                v1Last = v1;
                                v2Last = v2;
                                blockColourLast = blockColour;
                                ++bCount;
                            }
                        }
                    }

                    bc.setColour(block.delegate.name().toString(), String.valueOf(dv), blockColour);
                }
            }

            Logging.log("processed %d block textures, %d skipped, %d exceptions", bCount, sCount, eCount);
            genBiomeColours(bc);
        }
    }
}
